package com.keur.money.ui.market.fm

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import com.keur.money.R
import com.keur.money.databinding.FmMullaPaymentSuccessBinding
import com.keur.money.http.HttpHelper
import com.keur.money.mvp.ProFm
import com.keur.money.ui.market.act.KeurPaymentStatusAct
import com.keur.money.utils.event.KeurEventType
import com.keur.money.utils.helper.KeurCustomConfigsHelper
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/8/20 17:04
 * @Annotation：支付成功
 */
class KeurPaymentSuccessFm(): ProFm() {
    private var _binding: FmMullaPaymentSuccessBinding? = null
    private val binding get() = _binding ?: throw IllegalStateException(
        "Cannot payment success binding because it is null. Either the view hasn't been created yet, or it has already been destroyed."
    )
    var act: KeurPaymentStatusAct? = null
    private var orderData: JSONObject? = null

    constructor(act: KeurPaymentStatusAct, data: JSONObject): this(){
        this.act = act
        this.orderData = data
    }

    override fun getMullaRootView(inflater: LayoutInflater): View {
        _binding = FmMullaPaymentSuccessBinding.inflate(inflater)
        return binding.root
    }

    override fun initMullaView() {
        setMullaTitleBarVisibility(View.VISIBLE)
        setMullaTitleBarColor(Color.parseColor("#F8F8F8"))
        setMullaBackBtnRes(R.mipmap.mulla_back_black)
        setMullaTextTitle(getString(R.string.order_payment_status_title))
        setMullaRightBtnRes(R.mipmap.mulla_kefu_icon)
        _binding?.btnCopy?.visibility = View.GONE
    }

    override fun initMullaListener() {
        //客服
        rootViewBinding.ivFun.setOnClickListener {
            act?.let {
                showMullaKefuDialog("PaymentSuccess Page ${it.loadProductName()}",it.loadProductName(),it.loadProductToken())
                HttpHelper.addKeurEventAction(KeurEventType.CLICK.KEUR_CLICK_CUSTOMER,"PaymentSuccess Page ${it.loadProductName()}")
            }
        }

        _binding?.btnApply?.setOnClickListener {
            HttpHelper.addKeurEventAction(KeurEventType.CLICK.KEUR_CLICK_PAYMENT_SUCCESS_APPLY_AGAIN,"TransactionId：${orderData?.optString("transactionId")}")
            act?.showMullaProductsListAction()
        }
    }

    override fun initMullaData() {
        if (orderData != null){
            onRefreshData(orderData)
        }

        //牌照信息
        val licenseInfo = KeurCustomConfigsHelper.getMullaLicenseInfo()
        if (licenseInfo == null){
            _binding?.licenseLayout?.removeAllViews()
            _binding?.licenseLayout?.visibility = View.GONE
        } else {
            _binding?.licenseLayout?.removeAllViews()
            _binding?.licenseLayout?.addView(licenseView())
            _binding?.licenseLayout?.visibility = View.VISIBLE
        }
    }

    /**
     * 刷新数据
     */
    private fun onRefreshData(data: JSONObject?){
        data?.let {
            orderData = it
            _binding?.textAmount?.text = it.optString("realRepayAmount","")
            _binding?.textName?.text = it.optString("product","")
            _binding?.textThirdOrderId?.text = it.optString("transactionId","")
            _binding?.textCreateTime?.text = it.optString("createTime","")
            _binding?.textSuccessTime?.text = it.optString("realRepayTime","")
        }
    }

    override fun onStart() {
        super.onStart()
        act?.let {
            HttpHelper.addKeurEventAction(KeurEventType.PAGE.KEUR_PAGE_REPAYMENT_SUCCESS_START,it.loadProductName())
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        act?.let {
            HttpHelper.addKeurEventAction(KeurEventType.PAGE.KEUR_PAGE_REPAYMENT_SUCCESS_END,it.loadProductName())
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}